/**
 * 二维数组的方式。
 **/

#include <bits/stdc++.h>
using namespace std;
int dp[102][1002];
int W[102], V[102];
int T, M;
int main()
{
#ifdef LOCAL
    freopen("ALGO-30.in", "r", stdin);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> T >> M;
    for(int i = 1; i <= M; ++i)
        cin >> W[i] >> V[i];
    for(int i = 1; i <= M; ++i) 
    {
        for(int j = 1; j <= T; ++j) 
        {
            if(W[i] <= j)
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i]);
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
    cout << dp[M][T] << endl;
    return 0;
}